package com.zookeeper;

import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.zookeeper.CreateMode;

import java.util.List;

/**
 * Date: 2024/7/23 14:03
 * Author: T_log
 * Description: NodeDemo
 */
public class NodeDemo {

    public static void main(String[] args) throws Exception {
        RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
        CuratorFramework client = CuratorFrameworkFactory.newClient("localhost:2181", retryPolicy);


        client.start();
        System.out.println("已经成功的连接ZK服务了....");

        // 代码一、create()
        // 代码二、creatintParentsIfNeeded()
        client.create().creatingParentsIfNeeded()
                .withMode(CreateMode.PERSISTENT)
                .forPath("/my/path","100".getBytes());


        byte[] firstData = client.getData().forPath("/my/path");
        System.out.println("firstData:" + new String(firstData));



        // 代码五、setData,构造了一个SetDataBuilderImpl对象
        // 代码六、forpath()
        client.setData().forPath("/my/path", "100".getBytes());
        byte[] dataBytes = client.getData().forPath("/my/path");
        System.out.println(new String(dataBytes));


        client.setData().forPath("/my/path", "110".getBytes());
        byte[] dataBytes1 = client.getData().forPath("/my/path");
        System.out.println(new String(dataBytes1));


        List<String> children = client.getChildren().forPath("/my");
        System.out.println(children);


        client.delete().forPath("/my/path");


        client.close();
    }
}
